﻿using MySql.Data.MySqlClient;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.Entity.OrderMgr;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Domain.IRepository.OrderMgr;
using YunSpace.Repository.MiniSite;
using YunSpace.Repository.OrderMgr;

namespace YunSpace.Application.SiteLibraryManage
{
    public class OrdersApp
    {
        private ISiteOrderRepository service = new SiteOrderRepository();
        public List<OrderActionModel> GetList(Pagination pagination, string queryJson) {
            var result = new List<OrderActionModel>();
            using (var db=new RepositoryBase().BeginTrans())
            {
                var strSql = @"select * from ( select a.F_Id,'场地订单' F_Type,b.title SiteName,c.title SpaceName,a.F_StartTime,a.F_EndTime,d.F_UserName,a.F_OrderPrice,
                            a.F_Price,a.F_TotalPrice,a.F_Status,a.F_CreatorTime 
                            from MiniSite_SiteOrder a
                            inner join SL_Site b on a.F_SiteId=b.F_Id
                            inner join SL_Space c on a.F_SpaceId=c.F_Id
                            inner join MiniSite_Customer d on a.F_CustomerId=d.F_Id
                            where a.f_type=2 {0}
                            union all 
                            select a.F_Id,'活动订单' F_Type,b.F_Title SiteName,b.F_Subject SpaceName,b.F_StartTime,b.F_EndTime,d.F_UserName,a.F_OrderPrice,
                            a.F_Price,a.F_TotalPrice,a.F_Status,a.F_CreatorTime from  MiniSite_SiteOrder a   
                            inner join MiniSite_SiteActivity b on a.F_ActivityId=b.F_Id
                            inner join MiniSite_Customer d on a.F_CustomerId=d.F_Id  
                            where a.f_type=3 {0}) n order by F_CreatorTime desc
                           ";
                var queryParam = queryJson.ToJObject();
                DbParameter[] parameter = {
                    new MySqlParameter("@F_Status", queryParam["F_Status"].IsEmpty()?0:Convert.ToInt32(queryParam["F_Status"].ToString()))
                };
                if (!queryParam.IsEmpty())
                {
                    strSql = string.Format(strSql, queryParam["F_Status"].IsEmpty() ? "" : " and a.F_Status=@F_Status");
                }

                result = db.FindList<OrderActionModel>(strSql, parameter, pagination);
                db.Commit();
            }            
            return result;
        }

        public int ChangeDataState(string F_Id, string F_Status,string UserId) {
            try
            {
                OrderStatusRecordEntity orderrecord = new OrderStatusRecordEntity();
                SiteOrderEntity order = new SiteOrderEntity();
                using (var db = new RepositoryBase().BeginTrans())
                {
                    order.F_Id = F_Id;
                    order.F_Status = Convert.ToInt32(F_Status);
                    int count = service.Update(order);

                    orderrecord.Create();
                    orderrecord.f_orderid = order.F_Id;
                    orderrecord.f_operationtype = order.F_Status.ToString();
                    orderrecord.F_CreatorUserId = UserId;
                    db.Insert<OrderStatusRecordEntity>(orderrecord);

                    db.Commit();
                    return count;
                }
            }
            catch (Exception ex) {
                string msg = ex.Message;
                return 0;
            }
            
        }
    }
}
